function z2t,z,omega0,omegalambda,h

; computes the age of the Universe at a given redshift
; assuming a flat cosmology: much quicker than the general
; astrolib galage routine

; Check we're flat
if(omegalambda NE 0.0e0 and abs(omega0-(1.0e0-omegalambda)) GT 1.0e-6) then begin
    print," -z2t failed as OmegaLambda non-zero and geometry not flat"
    print,"Omega0      = ",omega0
    print,"Omega_tot   = ",omega0+omegalambda
    print,"Discrepancy = ",abs(omega0-(1.0e0-omegalambda))
    stop
endif

; Set Hubble Time
htime = 9.778e0 / h   ; Gyr

if(omegalambda EQ 0.0e0) then begin
    if(omega0 EQ 1.0e0)then begin
        ; EdS expansion law
        time = (2.0e0/3.0e0)*htime / (1.0e0+z)^(1.5e0)
    endif else begin
        if(omega0 GT 1.0e0)then begin
            ; Closed geometry
            omega0z = omega0*z
            factor1 = omega0/ (2.0e0*(omega0-1.0e0)^(1.5e0))
            term1=(omega0z-omega0+2.0e0)/(omega0z+omega0)
            term1=acos(term1)
            term2=2.0e0*sqrt((omega0-1.0e0)*(omega0z+1.0e0))/(omega0z+omega0)
            factor2=term1-term2
            time=factor1*factor2*htime
        endif else begin
            print," -age2z failed trying to integrate for open, matter dominated cosmology"
            stop
        endelse
    endelse
endif else begin
     ; Good old concordance stuff
    factor1 = (2.0e0/3.0e0) / sqrt(omegalambda)
    term1=sqrt(omegalambda/omega0)/((1.0e0+z)^1.5e0)
    term2=sqrt(1.0e0+((omegalambda/omega0)/((1.0e0+z)^3.0e0)))
    factor2=alog(term1+term2)
    time=factor1*factor2*htime
endelse
 
return,time

end
